import requests
from requests_toolbelt import MultipartEncoder
import os
from dotenv import load_dotenv

load_dotenv()

API_URL = "https://ai.gitee.com/v1/audio/transcriptions"
api_key = os.getenv("api_key")
headers = {
	"X-Failover-Enabled": "true",
	"Authorization": f"Bearer {api_key}" # 使用 Bearer 令牌进行身份验证
}

def query(payload, file_path):
	fields = [
		("model", payload["model"]),
		("file", (os.path.basename(file_path), open(file_path, "rb"), "audio/mpeg"))
	]
	encoder = MultipartEncoder(fields)
	headers["Content-Type"] = encoder.content_type
	response = requests.post(API_URL, headers=headers, data=encoder)
	return response.json()

file_path = "path/to/your/audio/file.mp3" # 替换为您本地的音频文件路径
output = query({
	"model": "whisper-large-v3-turbo"}, file_path)
print(output)